package com.jiezuo.auth.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jiezuo.auth.entity.Permission;
import com.jiezuo.auth.entity.Role;
import com.jiezuo.auth.entity.User;
import com.jiezuo.auth.mapper.RoleMapper;
import com.jiezuo.auth.service.RoleService;
import com.jiezuo.common.util.PageUtil;
import com.jiezuo.common.util.StringUtil;
import com.jiezuo.common.vo.PageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description
 * @Author 王帅
 * @Date 2020/7/29 10:14
 * @Version 1.0
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {

    @Autowired
    RoleMapper mapper;

    @Override
    public IPage<Role> findPage(PageVo pageVo, Role role) {
        QueryWrapper<Role> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete","0");
        if(StringUtil.isNotEmpty(role.getId())) queryWrapper.eq("id",role.getId());
        if(StringUtil.isNotEmpty(role.getRole_name())) queryWrapper.like("role_name",role.getRole_name());
        queryWrapper.orderByDesc("id");
        IPage<Role> page = this.page(PageUtil.initMpPage(pageVo), queryWrapper);
        for(Role r:page.getRecords()){
            List<Permission> permissions = mapper.findPermissionsByRoleId(r.getId());
            r.setMenus(permissions);
            List<User> users = mapper.findUsersByRoleId(r.getId());
            r.setUsers(users);
        }
        return page;
    }

    @Override
    public List<Role> findList(Role role) {
        QueryWrapper<Role> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete","0");
        if(StringUtil.isNotEmpty(role.getId())) queryWrapper.eq("id",role.getId());
        if(StringUtil.isNotEmpty(role.getRole_name())) queryWrapper.eq("role_name",role.getRole_name());
        List<Role> list = this.list(queryWrapper);
        for(Role r:list){
            List<Permission> permissions = mapper.findPermissionsByRoleId(r.getId());
            r.setMenus(permissions);
            List<User> users = mapper.findUsersByRoleId(r.getId());
            r.setUsers(users);
        }
        return list;
    }

    @Override
    public List<String> findCodeByUserId(String user_id) {
        return mapper.findCodeByUserId(user_id);
    }

}
